exports.fail_refund = function(req,res) {

    //获取restful参数
    var bodyParams = req.routeInfo.bodyParams;
    var resultData = {};
    var o_we_chat_no = 0;
    var o_wechat_pay = 0;
    var o_money = 0;
    var o_id = 0;
    var count = 0;
    var o_pay_status = 0;
    //任务流创建
    async.waterfall([

        function(cb){

            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.search_order+"/o_no/" + bodyParams.o_no ,
                    method : 'POST',
                    data:{
                        fields:[
                            "o_id",
                           "o_we_chat_no",//微信订单号
                            "o_wechat_pay",    //微信支付金额
                            "o_money",//订单价格
                        ].join(",")
                    }
                },
                function (err, data) {
                    o_we_chat_no = data.data[0].o_we_chat_no;
                    o_id = data.data[0].o_id;
                    o_wechat_pay = data.data[0].o_wechat_pay;
                    o_money = data.data[0].o_money;
                    o_pay_status = data.data[0].o_pay_status;
                    cb();
                }
            );

        },
        //判断用户是否参与成功
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.searchBulkPurchaseActivityUser+"/count/1/bpau_order_no/" + bodyParams.o_no ,
                    method : 'POST',
                    data:{
                        fields:[
                            "bpau_id",
                        ].join(",")
                    }
                },
                function (err, data) {
                    count = data.count;
                    if(count != 0 && o_we_chat_no != null && o_pay_status == 1 ){
                       return RES.response(res,false,"参与成功并且付款成功，所以不退款");
                    }
                    cb();
                }
            );
        },
        function(cb){
            R.SEND_HTTP(
                req,
                {
                    url: "http://localhost:3400/weixin/reimburse",
                    method: 'POST',
                    data: {
                        action: "tuikuan",
                        transaction_id: o_we_chat_no,
                        total_fee: o_money * 100,
                        refund_fee: o_wechat_pay*100
                    }

                },
                function (err, data) {
                    //console.log("微信退款处理....." + data);
                    //console.log(1);

                    R.SEND_HTTP(
                        req,
                        {
                            url: "http://127.0.0.1:3400/weixin/templateMessagesSent",
                            method: 'POST',
                            data  : {
                                openid :bodyParams.openid ,
                                type   : "pinTuanShiBai",
                                bookingName  : bodyParams.bookingName,
                                bookingCause : bodyParams.bookingCause
                            }
                        },
                        function (err, data) {

                            if( bodyParams.o_phone == "" ){
                                //return callback();
                                return cb();
                            }
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.sendCodeSms,
                                    method: 'POST',
                                    data  : {
                                        phone : bodyParams.o_phone,
                                        type  : 2,
                                        money :o_wechat_pay,
                                    }
                                },
                                function (err, data) {

                                    //发送短信
                                    ////console.log("已给" + item.o_name + "发送了短信");

                                    cb();

                                }
                            );
                        }
                    );

                }
            );
        },
        function(cb){//更新订单状态
            R.SEND_HTTP(
                req,
                {
                    url    : CORE_APIConfig.coreAPI.update_order,
                    method : 'POST',
                    data:{
                        o_no:bodyParams.o_no,
                        o_pay_status:6,
                        o_is_refund:1
                    }
                },
                function (err, data) {
                    if(err){

                        return RES.response(res,false,data);

                    }
                    //console.log(4);
                    cb();
                }
            );
        }

    ],function(err, result){
        //返回最终拼接结果
        RES.response(res,true,resultData);

    });
}